package servlets;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SomeServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //创建两个Cookie
        Cookie cookie = new Cookie("company","zhongshan");
        Cookie cookie1 = new Cookie("teacher","hhhhhh");

        //指定Cookie绑定的路径。注意，这里指定的路径要求必须要添加上项目名称
        cookie.setPath(req.getContextPath()+"/xxx/ooo/aaa");
        cookie1.setPath(req.getContextPath()+"/bbb");

// 设置Cookie的有效期。这个值为一个整型值，单位为秒
        // 该值大于0，表示将Cookie存放到客户端的硬盘
        // 该值小于0，与不设置效果相同，会将Cookie存放到浏览器的缓存
        // 该值等于0，表示Cookie一生成，马上失效
        // cookie.setMaxAge(60 * 60);  // 设置cookie的有效期为1小时
        // cookie2.setMaxAge(60 * 60 * 24 * 10);  // 设置cookie的有效期为10天







        //向响应中添加Cookie
        resp.addCookie(cookie);
        resp.addCookie(cookie1);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
